package org.openaltimeter.desktopapp;

import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.SpringLayout;
import javax.swing.JButton;
import javax.swing.border.EtchedBorder;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class AutoAnnotationsWindow extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	
	private final Controller controller;

	/**
	 * @param owner
	 */
	public AutoAnnotationsWindow(final Controller controller) {
		setTitle("Auto annotations");
		setModal(true);
		this.controller = controller;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(434, 266);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {			
			final JCheckBox chckbxFlightMaxHeight;
			final JCheckBox chckbxLaunchHeight;
			final JCheckBox chckbxZoomHeight;
			
			jContentPane = new JPanel();
			SpringLayout sl_jContentPane = new SpringLayout();
			jContentPane.setLayout(sl_jContentPane);
			
			JButton btnCancel = new JButton("Cancel");
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			sl_jContentPane.putConstraint(SpringLayout.SOUTH, btnCancel, -10, SpringLayout.SOUTH, jContentPane);
			sl_jContentPane.putConstraint(SpringLayout.EAST, btnCancel, -10, SpringLayout.EAST, jContentPane);
			jContentPane.add(btnCancel);
			
			JButton btnOk = new JButton("OK");
			sl_jContentPane.putConstraint(SpringLayout.SOUTH, btnOk, 0, SpringLayout.SOUTH, btnCancel);
			sl_jContentPane.putConstraint(SpringLayout.EAST, btnOk, -6, SpringLayout.WEST, btnCancel);
			jContentPane.add(btnOk);
			
			JPanel panel = new JPanel();
			sl_jContentPane.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, jContentPane);
			sl_jContentPane.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, jContentPane);
			sl_jContentPane.putConstraint(SpringLayout.SOUTH, panel, 120, SpringLayout.NORTH, jContentPane);
			sl_jContentPane.putConstraint(SpringLayout.EAST, panel, -10, SpringLayout.EAST, jContentPane);
			panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			jContentPane.add(panel);
			SpringLayout sl_panel = new SpringLayout();
			panel.setLayout(sl_panel);
			
			chckbxFlightMaxHeight = new JCheckBox("Maximum height");
			chckbxFlightMaxHeight.setSelected(true);
			sl_panel.putConstraint(SpringLayout.NORTH, chckbxFlightMaxHeight, 10, SpringLayout.NORTH, panel);
			sl_panel.putConstraint(SpringLayout.WEST, chckbxFlightMaxHeight, 10, SpringLayout.WEST, panel);
			panel.add(chckbxFlightMaxHeight);
			
			chckbxLaunchHeight = new JCheckBox("Launch height");
			chckbxLaunchHeight.setSelected(true);
			sl_panel.putConstraint(SpringLayout.NORTH, chckbxLaunchHeight, 6, SpringLayout.SOUTH, chckbxFlightMaxHeight);
			sl_panel.putConstraint(SpringLayout.WEST, chckbxLaunchHeight, 0, SpringLayout.WEST, chckbxFlightMaxHeight);
			panel.add(chckbxLaunchHeight);
			
			chckbxZoomHeight = new JCheckBox("Zoom height (for winch or high-start launches)");
			sl_panel.putConstraint(SpringLayout.NORTH, chckbxZoomHeight, 6, SpringLayout.SOUTH, chckbxLaunchHeight);
			sl_panel.putConstraint(SpringLayout.WEST, chckbxZoomHeight, 0, SpringLayout.WEST, chckbxFlightMaxHeight);
			panel.add(chckbxZoomHeight);
			
			JPanel panel_1 = new JPanel();
			sl_jContentPane.putConstraint(SpringLayout.SOUTH, panel_1, 45, SpringLayout.SOUTH, panel);
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			sl_jContentPane.putConstraint(SpringLayout.NORTH, panel_1, 10, SpringLayout.SOUTH, panel);
			sl_jContentPane.putConstraint(SpringLayout.WEST, panel_1, 0, SpringLayout.WEST, panel);
			sl_jContentPane.putConstraint(SpringLayout.EAST, panel_1, -10, SpringLayout.EAST, jContentPane);
			jContentPane.add(panel_1);
			
			JRadioButton rdbtnWholeDataset = new JRadioButton("Entire dataset");
			rdbtnWholeDataset.setSelected(true);
			sl_jContentPane.putConstraint(SpringLayout.NORTH, rdbtnWholeDataset, 10, SpringLayout.SOUTH, panel);
			sl_jContentPane.putConstraint(SpringLayout.WEST, rdbtnWholeDataset, 20, SpringLayout.WEST, jContentPane);
			
			final JRadioButton rdbtnSelectionOnly = new JRadioButton("Selection only");
			sl_jContentPane.putConstraint(SpringLayout.NORTH, rdbtnSelectionOnly, 10, SpringLayout.SOUTH, panel);
			sl_jContentPane.putConstraint(SpringLayout.WEST, rdbtnSelectionOnly, 207, SpringLayout.WEST, jContentPane);
			sl_jContentPane.putConstraint(SpringLayout.EAST, rdbtnSelectionOnly, -59, SpringLayout.EAST, jContentPane);
			sl_jContentPane.putConstraint(SpringLayout.EAST, rdbtnWholeDataset, -6, SpringLayout.WEST, rdbtnSelectionOnly);
			GroupLayout gl_panel_1 = new GroupLayout(panel_1);
			gl_panel_1.setHorizontalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_panel_1.createSequentialGroup()
						.addContainerGap()
						.addComponent(rdbtnWholeDataset)
						.addGap(63)
						.addComponent(rdbtnSelectionOnly, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
						.addGap(99))
			);
			gl_panel_1.setVerticalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_panel_1.createSequentialGroup()
						.addGap(5)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(rdbtnWholeDataset)
							.addComponent(rdbtnSelectionOnly)))
			);
			panel_1.setLayout(gl_panel_1);
			
			ButtonGroup group = new ButtonGroup();
			group.add(rdbtnSelectionOnly);
			group.add(rdbtnWholeDataset);
			
			final JCheckBox chckbxClearPreviousAnnotations = new JCheckBox("Clear previous annotations");
			chckbxClearPreviousAnnotations.setSelected(true);
			sl_jContentPane.putConstraint(SpringLayout.NORTH, chckbxClearPreviousAnnotations, 6, SpringLayout.SOUTH, panel_1);
			sl_jContentPane.putConstraint(SpringLayout.WEST, chckbxClearPreviousAnnotations, 0, SpringLayout.WEST, panel);
			jContentPane.add(chckbxClearPreviousAnnotations);
			
			btnOk.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					AutoAnnotationsSettings settings = new AutoAnnotationsSettings(chckbxFlightMaxHeight.isSelected(),
							chckbxLaunchHeight.isSelected(), chckbxZoomHeight.isSelected());				
					settings.setClearPreviousAnnotations(chckbxClearPreviousAnnotations.isSelected());
					
					controller.doAutoAnnotations(settings, rdbtnSelectionOnly.isSelected());
					dispose();
				}
			});
		}
		return jContentPane;
	}
}
